'use strict';

import Blog from './blog.model';
import BlogComment from './blog_comment.model';
import jwt from 'jsonwebtoken';

export function CreateBlog(req, res, next) {
	var newBlog = new Blog();
  newBlog.title = req.query.title;
  newBlog.author_id = req.query.author_id;
  newBlog.author_name = req.query.author_name;
  newBlog.content = req.query.content;
  newBlog.describe = req.query.describe;
  newBlog.save(function (err, data) {
  	if (err) {
  		res.json({stat: 1, err: error});
  	} else {
  		res.json({stat: 0});
  	}
	});
}

export function ListBlog(req, res, next) {
  Blog.find().sort({'create_time': 'desc'}).exec(function (err, data) {
    if (err) {
      res.json({stat: 1, err: err});
    } else {
      res.json({stat: 0, data: data});
    }
  });
}

export function DetailBlog(req, res, next) {
  var id = req.body.id;
  Blog.findById(id, function (err, data) {
    if (err) {
      res.json({stat: 1, err: err});
    } else {
      res.json({stat: 0, data: data});
    }
  });
}

export function CommentBlog(req, res, next) {
	var newBlogComment = new BlogComment();
  newBlogComment.content = req.query.title;
  newBlogComment.save(function (err, data) {
  	if (err) {
  		res.json({stat: 1, err: err});
  	} else {
  		var newBlog = new Blog();
		  newBlog.title = req.query.title;
		  Blog.findByIdAndUpdate(
		    "56b217a3c3b6ed2d2ffb1fe5", 
		  	{comment: data},
		  	function (err, raw) {
          console.log(err)
		  		if (err) res.json({stat: 1, err: err});
		  		else {
		  			res.json({stat: 0});
		  		}
				}
			);
  	}
  });
}


